#include "StdAfx.h"
#include "PatrolXmlConfigAdapter.h"
#include <vector>
#include <comutil.h>

CPatrolXmlConfigAdapter::CPatrolXmlConfigAdapter(LPCTSTR strTempName, DWORD dwTemp)
{
	strFileName = strTempName;
	dwType = dwTemp;
}

CPatrolXmlConfigAdapter::~CPatrolXmlConfigAdapter(void)
{
}

void CPatrolXmlConfigAdapter::AddData(const sPATROLPOS &PosPoint)
{
	m_PosList.push_back(PosPoint);
}

bool CPatrolXmlConfigAdapter::WriteDataToFile()
{
	HRESULT hr;
	MSXML2::IXMLDOMDocumentPtr pDoc;
	//const CLSID CLSID_DOMDocument;
	hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
    if(!SUCCEEDED(hr)) 
	{
		return (false); 
	} 

	MSXML2::IXMLDOMElementPtr  pDocElement;
	MSXML2::IXMLDOMElementPtr  pParElement;

	pDocElement=pDoc->createElement("path");
    pDoc->appendChild(pDocElement);

	pParElement = pDoc->createElement("point");
	CString strType;
	strType.Format(_T("%d"), dwType);
	pParElement->setAttribute("type", _variant_t(strType));
	pDocElement->appendChild(pParElement);

    std::vector<sPATROLPOS>::iterator iterpos;
    int nCount = 0;

	for (iterpos = m_PosList.begin(); iterpos != m_PosList.end(); iterpos++)
	{
		MSXML2::IXMLDOMElementPtr pPos;
		pPos = pDoc->createElement("patrolpos");
		
		CString strCount, strX, strY, strZ;
		nCount++;
		strCount.Format(_T("%d"), nCount);
		pPos->Puttext(_bstr_t(strCount));
		strX.Format(_T("%f"), iterpos->x); 
		strY.Format(_T("%f"), iterpos->y); 
		strZ.Format(_T("%f"), iterpos->z); 
		pPos->setAttribute("X", _variant_t(strX));
		pPos->setAttribute("Y", _variant_t(strY));
		pPos->setAttribute("Z", _variant_t(strZ));
		
		pParElement->appendChild(pPos);
	}

	pDoc->save(_variant_t(strFileName));
	return true;
}

bool CPatrolXmlConfigAdapter::ReadFile()
{
	HRESULT hr;
	MSXML2::IXMLDOMDocumentPtr pDoc;
	//const CLSID CLSID_DOMDocument;
	hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
    pDoc->load( _variant_t(strFileName));

	MSXML2::IXMLDOMElementPtr  pDocElement;
	MSXML2::IXMLDOMElementPtr  pParElement;

	pDocElement=pDoc->GetdocumentElement();
    if (pDocElement == NULL)
	{
		pDoc->save(_variant_t(strFileName));
	    return false;
	}
		

	if (pDocElement->hasChildNodes())
	{
		pParElement = pDocElement->selectSingleNode("point");

		if (pParElement == NULL)
		{
			pDoc->save(_variant_t(strFileName));
	        return false;
		}
	    
		CString strType;
		strType = (LPCTSTR)(_bstr_t)pParElement->getAttribute("type");
	    dwType = _tstoi(strType);

		MSXML2::IXMLDOMElementPtr pPos;
		MSXML2::IXMLDOMElementPtr pLastPos;
		if (pParElement->hasChildNodes())
		{
			CString strTemp, strLast;
			

            pPos = pParElement->GetfirstChild();
			strTemp = (LPCTSTR)pPos->Gettext();
	        pLastPos = pParElement->GetlastChild();
	        strLast = (LPCTSTR)pLastPos->Gettext();

	        do
		    {
			   CString strPos;
			   sPATROLPOS PatrolPos;
			   _variant_t  temp;

			   strPos = (LPCTSTR)(_bstr_t)pPos->getAttribute(_T("X"));
			 /*  temp = pPos->getAttribute(_T("x"));
			   strPos = temp.bstrVal;*/
			   PatrolPos.x = _tstof(strPos);

			   strPos = (LPCTSTR)(_bstr_t)pPos->getAttribute("Y");
			   PatrolPos.y = _tstof(strPos);

			   strPos = (LPCTSTR)(_bstr_t)pPos->getAttribute("Z");
			   PatrolPos.z = _tstof(strPos);

			   m_PosList.push_back(PatrolPos);
               
			   if (strTemp.Compare(strLast) == 0)
				   break;

			   if (pPos == NULL)
			   {
				   pDoc->save(_variant_t(strFileName));
	               return false;
			   }

			   pPos = pPos->GetnextSibling();
			   strTemp = (LPCTSTR)pPos->Gettext();
			}while(1);

			
		}
	}

	pDoc->save(_variant_t(strFileName));
	return true;
}

DWORD CPatrolXmlConfigAdapter::GetAttribute(void)
{
	return (dwType);
}

int CPatrolXmlConfigAdapter::GetSize(void)
{
	return (m_PosList.size());
}

bool CPatrolXmlConfigAdapter::GetPosData(sPATROLPOS &PosPoint, int nPos)
{
	if ((nPos > m_PosList.size()) || (nPos < 0))
		return false;

	PosPoint.x = m_PosList[nPos].x;
	PosPoint.y = m_PosList[nPos].y;
	PosPoint.z = m_PosList[nPos].z;
	return true;
}

void CPatrolXmlConfigAdapter::ClearList(void)
{
	m_PosList.clear();
}

bool CPatrolXmlConfigAdapter::WriteToLacol(std::vector<sPATROLPOS> &TempList)
{
	m_PosList = TempList;
	return (WriteDataToFile());
}

bool CPatrolXmlConfigAdapter::ReadToList(std::vector<sPATROLPOS> &TempList)
{
//	CFileFind Finder;
//    if (!Finder.FindFile(strFileName))
	if (!IsFileExist(strFileName))
    {
		TempList.clear();
	    return true;
    }

	if (!ReadFile())
		return false;

	TempList = m_PosList;
	return true;
}